package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

public class CustomerFieldFactory extends FieldFactory {
	
	public final TextField customerId = new TextField("Customer id");
	final TextField firstname = new TextField("Firstname");
	final TextField lastname = new TextField("Lastname");
	final TextField idNumber = new TextField("Id number");
	final TextField address = new TextField("Address");
	final TextField postcode = new TextField("Postcode");
	final TextField city = new TextField("City");
	final TextField tel1 = new TextField("Tel 1");
	final TextField tel2 = new TextField("Tel 2");

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("customerId".equals(propertyId)) {
			customerId.setRequired(true);
			customerId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return customerId;
		} if ("firstname".equals(propertyId)) {
			firstname.setRequired(true);
			firstname.setWidth(Constants.COMMON_FIELD_WIDTH);
			firstname.setRequiredError(firstname.getCaption()+"is required");
			firstname.setNullRepresentation("");
			return firstname;
		} if ("lastname".equals(propertyId)) {
			lastname.setRequired(true);
			lastname.setWidth(Constants.COMMON_FIELD_WIDTH);
			lastname.setRequiredError(lastname.getCaption()+" is required");
			lastname.setNullRepresentation("");
			return lastname;
		} if ("idNumber".equals(propertyId)) {
			idNumber.setRequired(true);
			idNumber.setWidth(Constants.COMMON_FIELD_WIDTH);
			idNumber.setRequiredError(idNumber.getCaption()+" is required");
			idNumber.setNullRepresentation("");
			return idNumber;
		} if ("address".equals(propertyId)) {
			address.setRequired(true);
			address.setWidth(Constants.COMMON_FIELD_WIDTH);
			address.setRequiredError(address.getCaption()+" is required");
			address.setNullRepresentation("");
			return address;
		} if ("postcode".equals(propertyId)) {
			postcode.setRequired(true);
			postcode.setWidth(Constants.COMMON_FIELD_WIDTH);
			postcode.setRequiredError(postcode.getCaption()+" is required");
			postcode.setNullRepresentation("");
			return postcode;
		} if ("city".equals(propertyId)) {
			city.setRequired(true);
			city.setWidth(Constants.COMMON_FIELD_WIDTH);
			city.setRequiredError(city.getCaption()+" is required");
			city.setNullRepresentation("");
			return city;
		} if ("tel1".equals(propertyId)) {
			tel1.setRequired(true);
			tel1.setWidth(Constants.COMMON_FIELD_WIDTH);
			tel1.setRequiredError(tel1.getCaption()+" is required");
			tel1.setNullRepresentation("");
			return tel1;
		} if ("tel2".equals(propertyId)) {
			tel2.setWidth(Constants.COMMON_FIELD_WIDTH);
			tel2.setNullRepresentation("");
			return tel2;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
